<?php
	function addButton($name)
	{
		$insert  = "INSERT INTO ".DB_PERFIX."buttons (`id`, `name`)"; 
		$insert .= "VALUES (NULL, '".$_POST['name']."');";
		$result = mysql_query($insert);
		if ($result > 0)
		{
			$result = mysql_query("SELECT id FROM ".DB_PERFIX."buttons WHERE name LIKE '".$_POST['name']."';");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['title'] = $name;
			}
		}
		return $item;
	}
	
	function getButtonAdmin($id)
	{
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."buttons WHERE id= ".$id.";");
		while($row = mysql_fetch_array($result))
		{
			foreach(array_keys($row) as $key)
			{ $button[$key] = $row[$key]; }
			$button['css'] = getCssArray(3, $row['id']);
		}
		return $button;
	}
	function getButtons($incat=0)
	{
		$i=0;
		if($incat > 0)
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."buttons WHERE id IN(SELECT item_id FROM ".DB_PERFIX."admin_hfb_cats_to_items WHERE type=1 AND cat_id=".$incat.");"); }
		else
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."buttons;"); }
		while($row = mysql_fetch_array($result))
		{
			foreach(array_keys($row) as $key)
			{ 
				$button[$row['id']][$key] = $row[$key]; 
				$i++;
			}
		}
		return $button;
	}
	function updateButton($id, $name, $height, $type=0)
	{
		$update  = 'UPDATE '.DB_PERFIX.'buttons SET ';
		$update .= 'name="'.$name.'", ';
		$update .= 'type='.$type.', ';
		$update .= 'height='.$height.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	function deleteButton($id)
	{
		if($id > 0)
		{
			$item = getButtonAdmin($id);
			if($item)
			{
				if($item['l_image'] != '')
				{ deleteButtonImage($id, 'l_image', $item['l_image']); }
				if($item['l_image_h'] != '')
				{ deleteButtonImage($id, 'l_image_h', $item['l_image_h']); }
				if($item['r_image'] != '')
				{ deleteButtonImage($id, 'r_image', $item['r_image']); }
				if($item['r_image_h'] != '')
				{ deleteButtonImage($id, 'r_image_h', $item['r_image_h']); }
				if($item['image'] != '')
				{ deleteButtonImage($id, 'image', $item['image']); }
				if($item['image_h'] != '')
				{ deleteButtonImage($id, 'image_h', $item['image_h']); }
				$result = mysql_query('DELETE FROM '.DB_PERFIX.'buttons WHERE id='.$id.';');
				return $result;
			}
		}
	}
	function deleteButtonImage($id, $field, $filename)
	{
		if (!unlink(BUTTONS_PATH.$filename))
		{ $status = "Error deleting ".BUTTONS_PATH.$filename; }
		else
		{ 
			$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET ".$field." = '' WHERE id=".$id.";");
			if ($result > 0)
			{$error[$field] = "Image Deleted";}
		}
		return $error;
	}
	function uploadButtonImages($id)
	{
		if (image_valid($_FILES['image']['type']) && ($_FILES['image']['size'] < 20000) && ($_FILES['image']['name'] != ""))
		{
			$temp_name = $_FILES["image"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$id."_bg.".$extension;
			
			if (file_exists(BUTTONS_PATH.$new_name))
			{$error['image'] = $_FILES["image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image"]["tmp_name"], BUTTONS_PATH.$new_name);
				$error['image'] = "Stored in: " . BUTTONS_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET image = '".$new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error['image'] = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['image_h']['type']) && ($_FILES["image_h"]["size"] < 20000) && ($_FILES['image_h']['name'] != ""))
		{
			$temp_name = $_FILES["image_h"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$id."_bg_h.".$extension;
			
			if (file_exists(BUTTONS_PATH.$new_name))
			{$error['image_h'] = $_FILES["image_h"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image_h"]["tmp_name"], BUTTONS_PATH.$new_name);
				$error['image_h'] = "Stored in: " . BUTTONS_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET image_h = '".$new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error['image_h'] = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['l_image']['type']) && ($_FILES["l_image"]["size"] < 20000) && ($_FILES['l_image']['name'] != ""))
		{
			$l_temp_name = $_FILES["l_image"]["name"];
			$l_extension = substr($l_temp_name,-3);
			$l_new_name = "type_".$id."_l.".$l_extension;
			
			if (file_exists(BUTTONS_PATH.$l_new_name))
			{$error['l_image'] = $_FILES["l_image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["l_image"]["tmp_name"], BUTTONS_PATH.$l_new_name);
				$error['l_image'] = "Stored in: " . BUTTONS_PATH.$l_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET l_image = '".$l_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error['l_image'] = "<strong>Image Uploaded ".$l_new_name."</strong>";}
			}
		}
		if (image_valid($_FILES['l_image_h']['type']) && ($_FILES["l_image_h"]["size"] < 20000) && ($_FILES['l_image_h']['name'] != ""))
		{
			$l_temp_name = $_FILES["l_image_h"]["name"];
			$l_extension = substr($l_temp_name,-3);
			$l_new_name = "type_".$id."_l_h.".$l_extension;
			
			if (file_exists(BUTTONS_PATH.$l_new_name))
			{$error['l_image_h'] = $_FILES["l_image_h"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["l_image_h"]["tmp_name"], BUTTONS_PATH.$l_new_name);
				$error['l_image_h'] = "Stored in: " . BUTTONS_PATH.$l_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET l_image_h = '".$l_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error['l_image_h'] = "<strong>Image Uploaded ".$l_new_name."</strong>";}
			}
		}
		if (image_valid($_FILES['r_image']['type']) && ($_FILES["r_image"]["size"] < 20000) && ($_FILES['r_image']['name'] != ""))
		{
			$r_temp_name = $_FILES["r_image"]["name"];
			$r_extension = substr($r_temp_name,-3);
			$r_new_name = "type_".$id."_r.".$r_extension;
			
			if (file_exists(BUTTONS_PATH.$r_new_name))
			{$error["r_image"] = $_FILES["r_image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["r_image"]["tmp_name"], BUTTONS_PATH.$r_new_name);
				$error["r_image"] = "Stored in: " . BUTTONS_PATH.$r_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET r_image = '".$r_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error["r_image"] = "<strong>Image Uploaded ".$r_new_name."</strong>";}
			}
		}
		if (image_valid($_FILES['r_image_h']['type']) && ($_FILES["r_image_h"]["size"] < 20000) && ($_FILES['r_image_h']['name'] != ""))
		{
			$r_temp_name = $_FILES["r_image_h"]["name"];
			$r_extension = substr($r_temp_name,-3);
			$r_new_name = "type_".$id."_r_h.".$r_extension;
			
			if (file_exists(BUTTONS_PATH.$r_new_name))
			{$error['r_image_h'] = $_FILES["r_image_h"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["r_image_h"]["tmp_name"], BUTTONS_PATH.$r_new_name);
				$error['r_image_h'] = "Stored in: " . BUTTONS_PATH.$r_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."buttons SET r_image_h = '".$r_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error['r_image_h'] = "<strong>Image Uploaded ".$r_new_name."</strong>";}
			}
		}
	}
?>